<?php


namespace app\admin\controller;



use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;

class SchoolController extends AdminBaseController
{
    //显示
    public  function index(){
        //接收查询条件
        $name = input('name');

        // halt($school);
        //连接学校表
        $school=db('school')
            ->alias('s')//重命名
            //搜索查询
            ->where(function (Query $query) use ($name) {
                //模糊查询
                if ($name) {
                    $query->where('name', 'like', "%$name%");
                }
            })
            ->join('jf_area a','a.areaId = s.province','LEFT')
            ->join('jf_area b','b.areaId = s.city','LEFT')
            ->join('jf_area c','c.areaId = s.district','LEFT')
            ->field('s.*,a.areaName a,b.areaName b,c.areaName c')

            //通过id倒序
            ->order('id desc')
            //每页显示3条
            ->paginate('3');
        //halt($school);
        $school->appends(['name' => $name]);
        //分页
        $page= $school ->render();
        //渲染模板
        $this->assign('school',$school);
        $this->assign('page',$page);

        //映射页面
        return $this->fetch();
    }

    //添加
    public function add(){
        $province=Db::name('area')
            ->where('level',1)
            ->all();
        $city=Db::name('area')
            ->where('level',2)
            ->all();
        $district=Db::name('area')
            ->where('level',3)
            ->all();
        $this->assign('province',$province);
        $this->assign('city',$city);
        $this->assign('district',$district);
        return $this->fetch();
    }

    //将添加数据入库
    public function addpost(){
        //接收数据
        $data= input();
        //halt($data);
        //判断学校名称是否为空
        if (empty($data['name'])){
            $this->error('学校名称不能空');
        }
        //添加创建时间
        //$data['create_time'] = date('Y-m-d H:m:s',time());
        //添加到数据库
        $add = db('school')->insert($data);
        if ($add){
            $this->success('添加成功','school/index');
        }else{
            $this->error('添加失败','school/index');
        }
    }

    //修改
    //修改
    public function edit(){
        //接收id
        $id = input('id');
        //通过id搜索该条数据
        $data= db('school')->get($id);

        $province=db('area')->where('level',1)->all();
        $city=db('area')->where('level',2)->all();
        $district=db('area')->where('level',3)->all();

        //渲染模板
        $this->assign('data',$data);
        $this->assign('province',$province);
        $this->assign('city',$city);
        $this->assign('district',$district);
        //映射页面
        return $this->fetch();
    }
    //保存修改信息
    public function editpost(){
        $data= input();
        // halt($data);
        if (empty($data['name'])){
            $this->error('学校名称不能空');
        }
        //添加到数据库
        $edit = db('school')->update($data);
        if ($edit){
            $this->success('修改成功','school/index');
        }else{
            $this->error('修改失败','school/index');
        }
    }

    //删除
    public function delete()
    {
        //查询要删除的id
        $id = $this->request->param('id', 0, 'intval');

        //通过id删除该条信息，并判断是否成功
        if (Db::name('school')->delete($id) !== false) {

            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }

    //根据省id查询该省下所有市
    public function getProvince(){
        $areaId=input('areaId');
        //halt($schoolId);
        $city=db('area')
            ->where('parentId',$areaId)->select();
        if ($city){
            $options=['<option value="">--请选择市--</option>'];
            foreach ($city as $v){
                $opt="<option value='".$v['areaId']."'>".$v['areaName']."</option>";
                array_push($options,$opt);
            }
            return $options;
        }else{
            return "<option value=''>暂无数据</option>";
        }
    }

//根据市id查询该市下所有区县
    public function getCity(){
        $areaId=input('areaId');
        //halt($schoolId);
        $district=db('area')
            ->where('parentId',$areaId)->select();
        if ($district){
            $options=['<option value="">--请选择区县--</option>'];
            foreach ($district as $v){
                $opt="<option value='".$v['areaId']."'>".$v['areaName']."</option>";
                array_push($options,$opt);
            }
            return $options;
        }else{
            return "<option value=''>暂无数据</option>";
        }
    }

}